package com.bc.bcapi.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.bc.bcmodel.req.*;
import com.bc.bcmodel.resp.AccountCreateResp;
import com.bc.bcmodel.resp.AccountInfoResp;
import com.bc.commoncore.util.JsonUtil;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * IXX钱包相关
 */
@Service
public class WalletIXXService {

    private String ixxDomain = "http://localhost:8080/";

    /**
     * 用户钱包注册，返还钱包地址
     */
    public AccountCreateResp create(AccountCreateReq req) {
        String url = ixxDomain + "account/address/query";
        String data = HttpUtil.post(url, BeanUtil.beanToMap(req));
        //判断响应状态
        if (!checkStatus(data)) {
            return null;
        }
        JSONObject jsonObject = JSONUtil.parseObj(data);
        return BeanUtil.toBean(jsonObject.get("data"), AccountCreateResp.class);
    }

    /**
     * 查询账户余额
     */
    public List<AccountInfoResp> walletInfo(AccountInfoReq req) {
        String url = ixxDomain + "account/wallet/list";
        String data = HttpUtil.post(url, BeanUtil.beanToMap(req));
        //判断响应状态
        if (!checkStatus(data)) {
            return null;
        }
        JSONObject jsonObject = JSONUtil.parseObj(data);
        return JsonUtil.str2list(jsonObject.get("data").toString(), AccountInfoResp.class);
    }

    /**
     * 充值回调
     */
    public void recharge(AccountRechargeReq req) {
        //这里是tokenrank自己内部的逻辑

    }

    /**
     * 交易
     */
    public boolean transfer(AccountTransferReq req) {
        String url = ixxDomain + "account/balance/workTransfer";
        String data = HttpUtil.post(url, BeanUtil.beanToMap(req));
        //判断响应状态
        if (!checkStatus(data)) {
            return false;
        }
        return true;
    }

    /**
     * 绑定提币地址
     */
    public boolean withdrawBind(AccountWithdrawBindReq req) {
        String url = ixxDomain + "account/withdraw/address/create";
        String data = HttpUtil.post(url, BeanUtil.beanToMap(req));
        //判断响应状态
        if (!checkStatus(data)) {
            return false;
        }
        return true;
    }

    /**
     * 删除提币地址,根据ID删除
     */
    public boolean withdrawRemove(AccountWithdrawCreateReq req) {
        String url = ixxDomain + "account/withdraw/address/delete";
        String data = HttpUtil.post(url, BeanUtil.beanToMap(req));
        //判断响应状态
        if (!checkStatus(data)) {
            return false;
        }
        return true;
    }

    /**
     * 申请提币
     */
    public boolean withdrawCreate(AccountWithdrawCreateReq req) {
        String url = ixxDomain + "account/withdraw/create";
        String data = HttpUtil.post(url, BeanUtil.beanToMap(req));
        //判断响应状态
        if (!checkStatus(data)) {
            return false;
        }
        return true;
    }

    /**
     * 申请提币
     */
    public boolean withdrawCancel(AccountWithdrawCancelReq req) {
        String url = ixxDomain + "account/withdraw/cancel";
        String data = HttpUtil.post(url, BeanUtil.beanToMap(req));
        //判断响应状态
        if (!checkStatus(data)) {
            return false;
        }
        return true;
    }

    private boolean checkStatus(String data) {
        if (StrUtil.isEmpty(data)) {
            return false;
        }
        JSONObject jsonObject = JSONUtil.parseObj(data);
        if (jsonObject == null) {
            return false;
        }
        if (jsonObject.get("code").equals("200")) {
            return true;
        }
        return false;
    }

}
